package org.webguitoolkit.project.query.impl;

import java.util.Collection;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;


import org.webguitoolkit.project.model.IIssue;
import org.webguitoolkit.project.model.impl.Issue;
import com.endress.infoserve.persistence.PersistenceManager;
import com.endress.infoserve.persistence.model.IPersistable;
import com.endress.infoserve.persistence.query.ICollectionQuery;
import com.endress.infoserve.persistence.query.hibernate.BaseHibernateQuery;

/**
 * Sample query to receive sample objects by name.
 */
public class ProjectByNameQuery extends BaseHibernateQuery implements
		ICollectionQuery<IIssue> {

	private String searchName;
	
	public ProjectByNameQuery(PersistenceManager pm, String searchName ) {
		super(pm);
		this.searchName = searchName;
	}

	@Override
	protected Class<? extends IPersistable> getSearchClass() {
		return Issue.class;
	}

	@SuppressWarnings( value="unchecked")
	public Collection<IIssue> execute() {
		Criteria criteria = getDBSession().createCriteria( getSearchClass() );
		if( searchName != null ){
			criteria.add(Restrictions.eq(Issue.ATTRIBUTE_NAME, searchName ));
		}
		criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
		List<IIssue> foundObjects = criteria.list();
		return foundObjects;
	}

}
